<?php

namespace App\Admin\Models;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Admin\Common as AdminCommon;

class SiteStore extends Model
{
    protected $table = 'site_store_list';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];



    /**
     *获取列表
     */
    public static function getList(Request $request)
    {

        $query = self::query()
            ->with(['companyinfo','adminuserinfo','brandinfo','businessmeminfo','customerserviceinfo','paytypeinfo'])
            ->where('is_del',1);



        // $info=AdminCommon::getCurrentAdmininfo($request);
        // if($info['type']!=0){//运维只可查看自己
        //     $query = $query->where(['admin_user_id'=>$info['id']]);
        // }




        $audit_status = $request->audit_status ?? 0;
        $isgetall = intval($request->isgetall) ?? 0;
        if (in_array($audit_status, [1, 2, 3, 4])) {
            $query->where('audit_status', $audit_status);
        }

        $contract_audit_status = $request->contract_audit_status ?? 0;
        if (in_array($contract_audit_status, [1, 2, 3, 4])) {
            $query->where('contract_audit_status', $contract_audit_status);
        }

        $is_can_use = $request->is_can_use ?? 0;
        if (in_array($is_can_use, [1, 2])) {
            $query->where('is_can_use', $is_can_use);
        }


        $p_code = $request->p_code;
        if ($p_code != '') {
            $query->where('p_code', $p_code);
        }

        $c_code = $request->c_code;
        if ($c_code != '') {
            $query->where('c_code', $c_code);
        }

        $d_code = $request->d_code;
        if ($d_code != '') {
            $query->where('d_code', $d_code);
        }

        $store_name = $request->store_name;
        if ($store_name != '') {
            $query->where('store_name', 'like', '%' . $store_name . '%');
        }
        // $business_mem_name = $request->business_mem_name;
        // if ($business_mem_name != '') {
        //     $query->where('business_mem_name', 'like', '%' . $business_mem_name . '%');
        // }

        // $contact_phone = $request->contact_phone;
        // if ($contact_phone != '') {
        //     $query->where('contact_phone', 'like', '%' . $contact_phone . '%');
        // }
        // $contact_name = $request->contact_name;
        // if ($contact_name != '') {
        //     $query->where('contact_name', 'like', '%' . $contact_name . '%');
        // }

        $company_name = $request->company_name;
        if ($company_name != '') {
            $cp_ids=SiteCompany::where('company_name', 'like', '%' . $company_name . '%')->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }

        $cp_id = intval($request->cp_id);
        if ($cp_id>0) {
            $query->where('cp_id', $cp_id);
        }

        $contact_phone = $request->contact_phone;
        if ($contact_phone != '') {
            $cp_ids=SiteCompany::where('contact_phone', $contact_phone)->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }


        $contact_name = $request->contact_name;
        if ($contact_name != '') {
            $cp_ids=SiteCompany::where('contact_name', $contact_name)->pluck('id');
            $query->whereIn('cp_id', $cp_ids);
        }

        //公司下拉多选--废弃
        // $querycp_ids= $request->cp_ids;
        // if($querycp_ids){
        //     $querycp_ids=is_array($querycp_ids)?$querycp_ids:(is_array(json_decode($querycp_ids,true))?json_decode($querycp_ids,true):[]);
        //     $query->whereIn('cp_id', $querycp_ids);
        // }



        $business_mem_name = $request->business_mem_name;
        if ($business_mem_name != '') {
            $business_mem_ids=BusinessMem::where('mem_name', 'like', '%' . $business_mem_name . '%')->pluck('id');
            $query->whereIn('business_mem_id', $business_mem_ids);
        }

        $customer_service_mem_name = $request->customer_service_mem_name;
        if ($customer_service_mem_name != '') {
            $customer_service_mem_ids=CustomerServiceMem::where('mem_name', 'like', '%' . $customer_service_mem_name . '%')->pluck('id');
            $query->whereIn('customer_service_id', $customer_service_mem_ids);
        }

        $brand_id = $request->brand_id;
        if ($brand_id != '') {
            $query->where('brand_id', $brand_id);
        }

        $contract_end_num_start=intval($request->contract_end_num_start);
        $contract_end_num_end=intval($request->contract_end_num_end);

        if($contract_end_num_start>0 && $contract_end_num_end>$contract_end_num_start){
            $contract_end_num_start_time=time()+$contract_end_num_start*86400;
            $contract_end_num_end_time=time()+$contract_end_num_end*86400;
            $query->where('contract_end_at', '>=', $contract_end_num_start_time)->where('contract_end_at', '<=', $contract_end_num_end_time);
        }

        $contract_start_time = strtotime($request->contract_start_time) > 0 ? strtotime($request->contract_start_time) : intval($request->contract_start_time);
        $contract_end_time = strtotime($request->contract_end_time) > 0 ? strtotime($request->contract_end_time) : intval($request->contract_end_time);
        if ($contract_start_time > 0 && $contract_start_time > 0) {
            $query->where('contract_at', '>=', $contract_start_time)->where('contract_at', '<=', $contract_end_time);
        }


        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('create_at', '>=', $start_time)->where('create_at', '<=', $end_time);
        }

        // 增加排序值
        $sortKey =$request->sort_key?? '';
        $sortVal=$request->sort_val?? '';
        if($request->type==1){
            if(!empty($sortKey)&& !empty($sortVal)){
                $query->orderBy($sortKey, $sortVal);
            }else{
                $query->orderBy('contract_end_at', 'asc');
            }
        }elseif($request->type==2){
            $query->orderBy('update_at', 'desc');
        } elseif($request->type==3){
            $query->orderBy('create_at', 'desc');
        }else{
            $query->orderBy('update_at', 'desc');
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function companyinfo(){
        return $this->hasOne(SiteCompany::class, 'id', 'cp_id');
    }

    public function adminuserinfo(){
        return $this->hasOne(AdminUser::class, 'id', 'admin_user_id');
    }

    public function brandinfo(){
        return $this->hasOne(Brand::class, 'id', 'brand_id');
    }

    public function businessmeminfo(){
        return $this->hasOne(BusinessMem::class, 'id', 'business_mem_id');
    }

    public function customerserviceinfo(){
        return $this->hasOne(CustomerServiceMem::class, 'id', 'customer_service_id');
    }

    public function paytypeinfo(){
        return $this->hasOne(AdminPaytypeList::class, 'id', 'pay_type');
    }

    public function contract()
    {
        return $this->hasMany(SiteStoreContract::class,'id','store_relation_id');
    }
}
